<?php
if (!defined('BASEPATH') &&
    strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) != 'xmlhttprequest')
    exit('Niente da vedere');

/*if( isset($_SERVER['HTTP_X_REQUESTED_WITH']) 
	&& strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest')
{*/
	include 'config.php';
	include 'functions.php';
    session_start();
    $cart = new Cart();
    $db = new db();
    
    if (  strcmp( $_POST['request_type'] , "add_book" ) == 0 ){
    	if ( $cart->is_already_in_cart($_POST['book_id']) ){ // $cart->is_already_in_cart($_POST['book_id']) )
			$book = $db->get_book($_POST['book_id']);
			$book['num_copies'] = $cart->modify_num_copies($book['id'] , $_POST['num_copies'], TRUE) ;
			$book['already_in_cart'] = true;
    		echo json_encode($book);
    	}
    	else{
    		
	    	$cart->add($_POST['book_id'] , $_POST['num_copies']);
	    	$book = $db->get_book($_POST['book_id']);
	    	$book['num_copies'] = $_POST['num_copies'];
	    	
	    	echo json_encode($book);
    	}
    }
    
    elseif ( strcmp( $_POST['request_type'] , "get_cart" ) == 0 ){
	    	if ( $cart->is_empty() )
	    		echo json_encode(array("empty_cart" => true));
	    	else{
	    		echo json_encode($cart->get_all());
	    	}
    }
    elseif ( strcmp( $_POST['request_type'] , "empty_cart" ) == 0 ){
    		$cart->empty_cart();
    		
    		echo json_encode(array("empty_cart" => true));
    }
    
    elseif ( strcmp( $_POST['request_type'] , "delete_book" ) == 0 ) {
    	$cart = new Cart();
    	$cart->remove($_POST['del_book_id']);
    	echo json_encode(array("deleted" => TRUE, "is_empty" => $cart->is_empty() ));
    }
	elseif ( strcmp( $_POST['request_type'] , "finalize_cart" ) == 0 ){
		foreach ($_POST as $key => $value){
			if ( strcmp("request_type" , $key ) != 0 ){
				$cart->modify_num_copies($key, $value, FALSE);
			}
		}
    	echo json_encode(array("finalize_cart" => true));
	}
	elseif ( strcmp( $_POST['request_type'] , "search" ) == 0 ){
		$search = strip_tags(str_ireplace("<", "", $_POST['query']));
		if ( strlen($search) == 0 ){
			echo json_encode( array( "have_results" => FALSE  ) );
			return;
		}
		 
		$query = "SELECT id, title, author, publisher, EAN, subject, media 
					FROM books WHERE title LIKE '%$search%' OR author LIKE '%$search%'
					OR publisher LIKE '%$search%' OR EAN LIKE '%$search%' ORDER BY title  ";
		
		$result = mysql_query($query);
		if ($result){
			if ( mysql_num_rows($result) > 0 ){ 
			$array = array();
			while( $row = mysql_fetch_array($result) ){
				$title = trim( $row['title'] );
				if ( !empty($title) )
	  				$array[] = $row;
	  		}
			
			echo json_encode( array( "have_results" => TRUE , "results_array" => $array) );
		}
		else echo json_encode( array( "have_results" => FALSE  ) );
		
		}
	
	}
	
	elseif ( strcmp( $_POST['request_type'] , "finalize_cart_2" ) == 0 ){
		require_once 'formvalidator.php';
			
		$validator = new FormValidator();
		
		$validator->addValidation("name","req","");
		$validator->addValidation("surname","req","");
		$validator->addValidation("tax_code","req","");
		$validator->addValidation("city","req","");
		$validator->addValidation("address","req","");
		$validator->addValidation("cap","req,num","");
		$validator->addValidation("phone","req","");
		$validator->addValidation("mobile","req","");
		$validator->addValidation("email","req","");
	
		if($validator->ValidateForm()){
			
			$request = array();
			$request['name'] = $_POST['name']; $request['surname'] = $_POST['surname'];
			$request['tax_code'] = $_POST['tax_code']; $request['city'] = $_POST['city']; 
			$request['address'] = $_POST['address']; $request['cap'] = $_POST['cap']; 
			$request['phone'] = $_POST['phone']; $request['mobile'] = $_POST['mobile']; 
			$request['email'] = $_POST['email']; $request['additional'] = $_POST['additional'];
			
			
			$db->insert_request($request);
			echo json_encode(array( "request_completed" => true ));
			
			
			send_request($request);
			$cart->empty_cart();
			
		}
		
	}
	elseif ( strcmp( $_POST['request_type'] , "login" ) == 0 ){
		$email = $_POST['email'];
		$password = $_POST['password'];
		echo json_encode(login( $email , $password ));
	
	}
	elseif ( strcmp( $_POST['request_type'] , "change_page" ) == 0 ){
		$subject = $_POST['subject'];
		$page = $_POST['page_num'];
		
		
		
		$books = $db->get_all_books($subject, "title", $page, FALSE);
			
			echo json_encode( array( "have_results" => TRUE , "results_array" => $books['list']) );
			
	
	}
	elseif( strcmp( $_POST['request_type'] , "sell_books" ) == 0 ){
		require_once 'formvalidator.php';
			
		$validator = new FormValidator();
		
		$validator->addValidation("name","req","");
		$validator->addValidation("surname","req","");
		$validator->addValidation("tax_code","req","");
		$validator->addValidation("city","req","");
		$validator->addValidation("address","req","");
		$validator->addValidation("cap","req,num","");
		$validator->addValidation("phone","req","");
		$validator->addValidation("mobile","req","");
		$validator->addValidation("email","req","");
		$validator->addValidation("book_textarea","req","");
		
		if($validator->ValidateForm()){
			
			$request = array();
			$request['name'] = $_POST['name']; $request['surname'] = $_POST['surname'];
			$request['tax_code'] = $_POST['tax_code']; $request['city'] = $_POST['city']; 
			$request['address'] = $_POST['address']; $request['cap'] = $_POST['cap']; 
			$request['phone'] = $_POST['phone']; $request['mobile'] = $_POST['mobile']; 
			$request['email'] = $_POST['email']; $request['book_list'] = $_POST['book_textarea'];
		
			extract($request);
			//email send
			$message="<!DOCTYPE HTML PUBLIC '-//W3C//DTD HTML 4.01 Transitional//EN'>
				<html><head><meta http-equiv='Content-Type' content='text/html; charset=iso-8859-1'>
				<title>Richiesta vendita libri</title></head>
				<body style='margin:0px;'>
				<h3>Un cliente propone la vendita di libri</h3>
				<p style='margin-right:10px;'><b>Nome:</b></p><p>$name</p>
				<p style='margin-right:10px;'><b>Cognome:</b></p><p>$surname</p>
				<p style='margin-right:10px;'><b>Codice fiscale:</b></p><p>$tax_code</p>
				<p style='margin-right:10px;'><b>Città:</b></p><p>$city</p>
				<p style='margin-right:10px;'><b>Indirizzo:</b></p><p>$address</p>
				<p style='margin-right:10px;'><b>CAP:</b></p><p>$cap</p>
				<p style='margin-right:10px;'><b>Telefono:</b></p><p>$phone</p>
				<p style='margin-right:10px;'><b>Cellulare:</b></p><p>$mobile</p>
				<p style='margin-right:10px;'><b>Email:</b></p><p>$email</p>
				<p style='margin-right:10px;'><b>Richiesta:</b></p><p>$book_list</p>
				<br>
				<h3>Rispondendo a questa mail contatterai direttamente il cliente.</h3>
				
				</body></html>";
			$from = $email;
			$subject = "Mercatinoscolastico.it : Un cliente vuole vendere dei libri";
			$to_admin = site_email();
			$headers  = "From: $from\r\n"; 
	    	$headers .= "Content-type: text/html\r\n"; 
	    	$sent = mail($to_admin, $subject, $message, $headers) ;
	    	
	    	echo json_encode(array("request_sent" => $sent ) );
	    	
		}
	}
	

?> 